﻿using System.Windows;
using System.Data.Objects;
using System.Linq;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;

namespace Harmonic.AdministratorTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GetUsers();
        }


        public void GetUsers()
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            if (bw.IsBusy != true)
            {
                bw.RunWorkerAsync();
            }
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.grdUsers.ItemsSource = (IList<ReadModel.User>)e.Result;
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = (IList<ReadModel.User>)ReadModel.Queries.GetUsers();
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            GetUsers();
        }

        private void grdUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ReadModel.User selectedUser = (ReadModel.User)grdUsers.SelectedItem;
                if (selectedUser == null) return;

                if (selectedUser.State == "Active")
                    txtUpgradeDowngrade.Text = "Upgrade to Priority User";
                else
                    txtUpgradeDowngrade.Text = "Downgrade to Active User";
            }
            catch
            {
            }
        }

        private void btnUpgradeDowngrade_Click(object sender, RoutedEventArgs e)
        {
            ChangeUserStatus changeUserStatus = new ChangeUserStatus();
            ReadModel.User selectedUser = (ReadModel.User)grdUsers.SelectedItem;
            if (selectedUser == null) return;
            changeUserStatus.ShowForm(selectedUser.Id, selectedUser.Version,
                selectedUser.State == "Active" ? 
                    ChangeUserStatus.StateChangeType.Upgrade : 
                    ChangeUserStatus.StateChangeType.Downgrade);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (grdUsers.SelectedItem == null) return;

            ChangeUserStatus changeUserStatus = new ChangeUserStatus();
            ReadModel.User selectedUser = (ReadModel.User)grdUsers.SelectedItem;
            changeUserStatus.ShowForm(selectedUser.Id, selectedUser.Version,                
                    ChangeUserStatus.StateChangeType.Delete);
        }

    }
}
